package com.webdesign.action;

import java.io.File;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.webdesign.dto.DisplayDTO;
import com.webdesign.dto.HomeworkInfoDTO;
import com.webdesign.dto.ModifyHomeworkDTO;
import com.webdesign.dto.MyPage;
import com.webdesign.entity.TbTerm;
import com.webdesign.service.ITbAdminService;
import com.webdesign.service.ITbDisplyService;
import com.webdesign.service.ITbHomeworkService;
import com.webdesign.service.ITbTermService;
import com.webdesign.service.ITbUserhomeworkService;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

@Controller("SetDisplyAction")
@Scope("prototype")
public class SetDisplyAction extends ActionSupport{
	
	private ITbUserhomeworkService iTbUserhomeworkService;
	
	private ITbDisplyService iTbDisplayService;
	
	private String userhomeworkid;
	
	private Boolean flag;
	
	private ITbAdminService iTbAdminService;
	
	private List<DisplayDTO> dsplyhws;
	
	private String numpage;
	
	private MyPage page;
	
	private Map root = new HashMap();
	
	private String propath = ServletActionContext.getRequest().getRealPath("");
	
	private String path = ServletActionContext.getRequest().getContextPath();
	
	private Map<String,Object> session = ActionContext.getContext().getSession();
	
	private String username = (String) session.get("username");
	
	private Configuration cfg = new Configuration();
	
	/*
	 * 唐  5.25 管理员设置优秀作业
	 */
	
	public String setDisplyShow() throws IOException, TemplateException{
		dsplyhws = iTbAdminService.displyShow();
		
		if(page == null){
			page = new MyPage();
			page.setNumber(1);
		}
		
		Integer count = dsplyhws.size();
		page.setTotalElements(count);
		if(numpage != null){
			page.setNumber(Integer.parseInt(numpage));
		}
		Integer sItem = (int)(page.getNumber() - 1) * page.getSize();
		Integer eItem = sItem + page.getSize();
		if(eItem > count){
			dsplyhws = dsplyhws.subList(sItem, count);
		}else{
			dsplyhws = dsplyhws.subList(sItem, eItem);
		}
		
		cfg.setDirectoryForTemplateLoading(new File(propath + "/template"));
		root.put("path", path);
		root.put("username", username);
		root.put("page", page);
		root.put("dsplyhws", dsplyhws);
		
		Template template = cfg.getTemplate("/admin/setdisplay.ftl");
		Writer out = new OutputStreamWriter(System.out); 
		template.process(root, out);
		out.flush();
		return "success";
	}
	
	
	public String setDisply() throws IOException, TemplateException{
		String[] homeworkid = userhomeworkid.split(",");
		flag = iTbAdminService.setDisplayToHP(homeworkid);
		if(flag){
			return "success";
		}else{
			return "error";
		}
		
	}
	
	
	public ITbUserhomeworkService getiTbUserhomeworkService() {
		return iTbUserhomeworkService;
	}

	@Resource(name="TbUserhomeworkService")
	public void setiTbUserhomeworkService(
			ITbUserhomeworkService iTbUserhomeworkService) {
		this.iTbUserhomeworkService = iTbUserhomeworkService;
	}
	
	

	public ITbDisplyService getiTbDisplayService() {
		return iTbDisplayService;
	}

	@Resource(name="TbDisplyService")
	public void setiTbDisplayService(ITbDisplyService iTbDisplayService) {
		this.iTbDisplayService = iTbDisplayService;
	}
	
	public ITbAdminService getiTbAdminService() {
		return iTbAdminService;
	}

	public Map getRoot() {
		return root;
	}


	public void setRoot(Map root) {
		this.root = root;
	}
	
	public String getUserhomeworkid() {
		return userhomeworkid;
	}


	public void setUserhomeworkid(String userhomeworkid) {
		this.userhomeworkid = userhomeworkid;
	}

	public Boolean getFlag() {
		return flag;
	}


	public void setFlag(Boolean flag) {
		this.flag = flag;
	}


	@Resource(name="TbAdminService")
	public void setiTbAdminService(ITbAdminService iTbAdminService) {
		this.iTbAdminService = iTbAdminService;
	}


}
